package ch.mueller.android.todo.view;

import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import ch.mueller.android.todo.R;
import ch.mueller.android.todo.controller.ToDo_SQLOpenHelper;
import ch.mueller.android.todo.model.TodoItems;

public class AddToDo extends Activity {

    private EditText text, priority;
    private int id = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.add_todo);

        this.priority = (EditText) findViewById(R.id.ex_addtodo_priority);
        this.text = (EditText) findViewById(R.id.ex_addtodo_text);

        if (getIntent().getExtras() != null) {
            try {
                Button button = (Button) findViewById(R.id.bu_todo_delete);
                button.setEnabled(true);
                this.priority.setText(getIntent().getExtras().getInt(TodoItems.TC_PRIORITY) + "");
                this.text.setText(getIntent().getExtras().getString(TodoItems.TC_TITLE) + "");
                this.id = getIntent().getExtras().getInt(TodoItems.TC_ID);
            } catch (Exception e) {
                Toast.makeText(this, "Some shit didn't work", Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void onButtonClick(View view) {

        SQLiteDatabase database = new ToDo_SQLOpenHelper(this).getReadableDatabase();

        switch (view.getId()) {
        case R.id.bu_todo_save:

            if ((!(text.getText().toString().equals(""))) && (!(priority.getText().toString().equals("")))) {

                try {

                    ContentValues values = new ContentValues();

                    values.put(TodoItems.TC_TITLE, text.getText().toString());
                    values.put(TodoItems.TC_PRIORITY, priority.getText().toString());
                    database.insert(TodoItems.TABLE_NAME, null, values);

                    Toast.makeText(this, "New Todo insert", Toast.LENGTH_SHORT).show();

                } catch (Exception e) {
                    Toast.makeText(this, "db instert failed", Toast.LENGTH_SHORT).show();
                }
                this.finish();
            }else {
                Toast.makeText(this, "Insert text and priority", Toast.LENGTH_SHORT).show();                
            }
            break;
        case R.id.bu_todo_delete:

            SQLiteDatabase database2 = new ToDo_SQLOpenHelper(this).getReadableDatabase();

            database2.delete(TodoItems.TABLE_NAME, TodoItems.TC_ID + " = " + id, null);

            database2.close();

            this.finish();

            Toast.makeText(this, "Deleted", Toast.LENGTH_SHORT).show();
        }
    }
}
